import React from 'react';
const R = require('ramda');
import ReactDOM from 'react-dom';
import { Table, Row, Col, Form, Input, Icon, InputNumber, Radio, DatePicker, Select, Upload, Button, Switch, Modal, Popconfirm, Tabs, message } from 'antd';
import BizParams from '../../../components/commons/BizParams'
import PortSelect from '../../../components/commons/PortSelect'
import CurrencySelect from '../../../components/commons/CurrencySelect'
import AreaSelect from '../../../components/commons/AreaSelect'

import EditCustomer from '../components/EditCustomer';
import Agencyagreement from '../components/Agencyagreement';
import AgreementInfo from '../components/AgreementInfo';
import Linkman from '../components/Linkman';
import RecAcc from '../components/RecAcc';
import Buyer from '../components/Buyer';
import Product from '../components/Product';
import Drawer from '../components/Drawer';
import Order from '../components/Order';
import MarketInfo from '../components/MarketInfo';
import BusinessAgreement from '../components/BusinessAgreement';
import StatementList from '../statement/StatementList';
import ReconciliationList from '../components/ReconciliationList';
import OrderBilldetailList from '../components/OrderBilldetailList';
import H2HAccountList from '../components/H2HAccountList';
import moment from 'moment';
import RightBox from "../../commons/RightBox";
import RIGHT from "../../../utils/RightConstants";
import Constants from "../../../utils/Constants";
import ExtraInfo from '../components/extraInfo';

moment.locale('zh-cn');

const TabPane = Tabs.TabPane;
const FormItem = Form.Item;

export default class CustomerDetail extends React.Component {

    constructor(props) {
        super(props);
        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource;
        this.state = {
            searchConditions: {},
            open: false,
            disabled: true,
            tabActiveKey: "1",
            cstid: "",
            editCustomerDisable: false,
            showOther: false,
            extraUpdating: false    //客户交接单是否再次编辑中
        };
    }


    handleOpen = (disabled, searchConditions, tabActiveKey, cstid, editCustomerDisablel, type) => {
        type = R.isNil(type) ? "customerManage" : type;

        let showOther = false;

        if ("customerDetail" == type) {
            showOther = true;
        }

        this.setState({
            open: true, disabled: disabled, searchConditions: searchConditions,
            tabActiveKey: tabActiveKey, cstid: cstid, editCustomerDisable: editCustomerDisablel,
            showOther: showOther
        });

    }


    handleResetFields = () => {
        this.state.cstid = "";
        if (this.editCustomer) this.editCustomer.handleResetFields();
        if (this.linkman) this.linkman.handleResetFields();
        if (this.agencyagreement) this.agencyagreement.handleResetFields();
        if (this.agreementInfo) this.agreementInfo.handleResetFields();
        if (this.recAcc) this.recAcc.handleResetFields();
        if (this.buyer) this.buyer.handleResetFields();
        if (this.product) this.product.handleResetFields();
        if (this.drawer) this.drawer.handleResetFields();
        if (this.order) this.order.handleResetFields();
        if (this.marketInfo) this.marketInfo.handleResetFields();
        if (this.extraInfo) this.extraInfo.handleResetFields();
    }

    handleSaveAndSubmit = (status) => {
        if (this.state.tabActiveKey === "1") {
            if (this.editCustomer) {
                this.editCustomer.checkCustomerInfo(this.props.dispatch, this.onSuccess, this.state.cstid, status)

            }

        }
    }

    onSuccess = () => {
        this.props.handleSearch();
        this.handleResetFields();
        setTimeout(this.handleCancel, 1000);
    }


    handleSaveAttachment = () => {
        if (this.state.tabActiveKey === "3") {
            if (this.agreementInfo) {
                this.agreementInfo.handleSaveAttachment();
            }

        }
    }

    handleSaveExtra = () => {
        this.extraInfo.handleConfirm();
    }

    handleCancel = (flag) => {
        if (flag) {
            this.handleResetFields();
        }
        this.setState({ tabActiveKey: "1", open: false, searchConditions: {} });
        if (!R.isNil(this.businessAgreement) && !R.isEmpty(this.businessAgreement)) {
            this.businessAgreement.handleResetFields();
        }

    }

    handleChangeTabe = (key) => {
        this.setState({ tabActiveKey: key })
    }

    handleReUpdateExtra = (extraUpdating) => {
        this.setState({ extraUpdating })
    }

    render() {

        let footer = [];

        let dataSource = R.isNil(this.props.dataSource) ? {} : this.props.dataSource;
        let approvalTit = dataSource && dataSource.businessType === "2" ? "保存并提交" : "保存并提交审核";

        if ((this.state.tabActiveKey === "1") && !this.state.editCustomerDisable) {
            if (dataSource.csttype === "") {
                footer.push(<Button key="submit" size="large" loading={this.state.submitLoading}
                    disabled={this.state.disabled} onClick={this.handleSaveAndSubmit.bind(this, null)}
                    style={{ display: this.state.tabActiveKey === "1" || this.state.tabActiveKey === "3" || this.state.disabled ? "inline-block" : "none" }}>保存</Button>);
                footer.push(<Button key="primary" size="large" loading={this.state.submitLoading}
                    disabled={this.state.disabled} onClick={this.handleSaveAndSubmit.bind(this, 2)}
                    style={{ display: this.state.tabActiveKey === "1" || this.state.disabled ? "inline-block" : "none" }}>{approvalTit}</Button>);
            } else {
                footer.push(<Button key="submit" size="large" loading={this.state.submitLoading}
                    disabled={this.state.disabled} onClick={this.handleSaveAndSubmit.bind(this, null)}
                    style={{ display: this.state.tabActiveKey === "1" || this.state.tabActiveKey === "3" || this.state.disabled ? "inline-block" : "none" }}>保存</Button>);
                footer.push(<Button key="primary" size="large" loading={this.state.submitLoading}
                    disabled={this.state.disabled} onClick={this.handleSaveAndSubmit.bind(this, 2)}
                    style={{ display: this.state.tabActiveKey === "1" || this.state.disabled ? "inline-block" : "none" }}>提交</Button>);
            }
        }

        if ((this.state.tabActiveKey === "3" && this.state.editCustomerDisable != true)) {
            let rights = R.isNil(sessionStorage.getItem(Constants.USERRIGHTS)) ? [] : sessionStorage.getItem(Constants.USERRIGHTS);
            if (R.contains(RIGHT.CUSTOMER.CUSTOMER_MANAGE_ATTACH_SAVE)(rights)) {
                footer.push(<Button key="submit" size="large" loading={this.state.submitLoading} disabled={this.state.disabled} onClick={this.handleSaveAttachment.bind(this)}
                    style={{ display: this.state.tabActiveKey === "1" || this.state.tabActiveKey === "3" || this.state.disabled ? "inline-block" : "none" }}>保存附件</Button>);
            }
        }
        if (this.state.tabActiveKey === "16") {
            if (this.props.extra && !this.state.extraUpdating) {
                let rights = R.isNil(sessionStorage.getItem(Constants.USERRIGHTS)) ? [] : sessionStorage.getItem(Constants.USERRIGHTS);
                if (R.contains(RIGHT.CUSTOMER.CUSTOMER_MANAGE_EXTRA_UPDATE)(rights)) {
                    footer.push(<Button key="update" size="large" onClick={this.handleReUpdateExtra.bind(this, true)}>重新编辑</Button>);
                }
            } else {
                let rights = R.isNil(sessionStorage.getItem(Constants.USERRIGHTS)) ? [] : sessionStorage.getItem(Constants.USERRIGHTS);
                if (R.contains(RIGHT.CUSTOMER.CUSTOMER_MANAGE_EXTRA_SAVE)(rights)) {
                    footer.push(<Button key="submit" size="large" loading={this.state.submitLoading} onClick={this.handleSaveExtra.bind(this)}>保存</Button>);
                }
            }
        }

        footer.push(<Button key="cancel" size="large" disabled={this.state.disabled} onClick={this.handleCancel.bind(this, true)}>取消</Button>);


        let widths = { "1": 900, "2": 1000, "3": 1080, "4": 900, "5": 900, "6": 900, "7": 900, "8": 900, "9": 900, "10": 900, "11": 900, "12": 1000, "13": 1300, "14": 1300, "15": 900, "16": 1000 };

        let reason = (R.isEmpty(this.props.approvaList) || R.isNil(this.props.approvaList)) ? "" : "__审核备注:" + this.props.approvaList[0].memo;


        let fkManager = R.isNil(this.editCustomer) ? null : R.isNil(this.editCustomer.props.dataSource) ? null : this.editCustomer.props.dataSource.fkmanager;
        return (
            <Modal maskClosable={false} footer={this.state.disabled ? null : footer} visible={this.state.open} title={this.state.showOther == true ? "客户详情" + reason : "编辑客户" + reason} width={widths[this.state.tabActiveKey]} wrapClassName="vertical-center-modal" onCancel={this.handleCancel}>
                <Tabs activeKey={this.state.tabActiveKey} tabPosition={'left'} onChange={this.handleChangeTabe.bind(this)} >
                    <TabPane tab="基本信息" key="1">{<EditCustomer wrappedComponentRef={(inst) => this.editCustomer = inst}
                        disabled={this.state.editCustomerDisable} type={"edit"}
                        manager={this.props.manager}
                        agencyagreementList={this.props.agencyagreementList}
                        dataSource={this.props.dataSource}
                        blacklist={this.props.blacklist}
                        dispatch={this.props.dispatch}
                        factoryUser={this.props.factoryUser} businessType={dataSource && dataSource.businessType ? dataSource.businessType : ""}
                    />}</TabPane>

                    <TabPane tab="合作信息" key="2">{<Agencyagreement
                        wrappedComponentRef={(inst) => this.agencyagreement = inst}
                        disabled={this.state.editCustomerDisable}
                        cstid={this.state.cstid}
                        dispatch={this.props.dispatch} dataSource={this.props.agencyagreementList}
                        agencyagreement={this.props.agencyagreement}
                        agencyagreementPage={this.props.agencyagreementPage}
                        agencyagreementList={this.props.agencyagreementList}
                        customer={this.props.dataSource}
                        manager={this.props.manager}
                        showPA={this.props.showPA} />}</TabPane>

                    {R.equals(dataSource.csttype, "2") ? "" :
                        <TabPane tab="协议与资质" key="3">{
                            <AgreementInfo
                                wrappedComponentRef={(inst) => this.agreementInfo = inst}
                                disabled={this.state.editCustomerDisable}
                                dispatch={this.props.dispatch}
                                cstid={this.state.cstid}
                                dataSource={this.props.attachments}
                                cstInfo={this.props.dataSource}
                                customerHandleCancel={this.handleCancel} />
                        }
                        </TabPane>
                    }


                    {R.equals(dataSource.csttype, "2") ? "" :
                        <TabPane tab="业务合同管理" key="10">{<BusinessAgreement
                            wrappedComponentRef={(inst) => this.businessAgreement = inst}
                            disabled={false}
                            businessAgreement={this.props.businessAgreement}
                            cstid={this.state.cstid} dispatch={this.props.dispatch}
                            dataSource={this.props.dataSource}
                            fkManager={fkManager}

                        />}</TabPane>
                    }


                    <TabPane tab="联系人信息" key="4">{<Linkman wrappedComponentRef={(inst) => this.linkman = inst}
                        disabled={this.state.disabled}
                        linkmanPage={this.props.linkmanPage}
                        linkmanList={this.props.linkmanList}
                        cstid={this.state.cstid} dispatch={this.props.dispatch}
                        linkman={this.props.linkman} />}</TabPane>

                    <TabPane tab="营销线索" key="15">{<MarketInfo wrappedComponentRef={(inst) => this.marketInfo = inst}
                        disabled={this.state.disabled}
                        cstid={this.state.cstid}
                        dispatch={this.props.dispatch}
                        marketInfoPage={this.props.marketInfoPage}
                        marketInfoList={this.props.marketInfoList}
                        marketInfo={this.props.marketInfo} />}</TabPane>

                    <TabPane tab="客户交接单" key="16">{<ExtraInfo wrappedComponentRef={(inst) => this.extraInfo = inst}
                        disabled={this.state.disabled} extraUpdating={this.state.extraUpdating}
                        cstid={this.state.cstid} dispatch={this.props.dispatch} handleReUpdateExtra={this.handleReUpdateExtra}
                        dataSource={this.props.extra} />}</TabPane>

                    {this.state.showOther == true && dataSource.csttype !== '2' ?
                        <TabPane tab="收款人账户" key="5">{<RecAcc wrappedComponentRef={(inst) => this.recAcc = inst}
                            disabled={this.state.disabled}
                            recAccPage={this.props.recAccPage}
                            recAccList={this.props.recAccList}
                            cstid={this.state.cstid} dispatch={this.props.dispatch}
                            recAcc={this.props.recAcc} />}</TabPane>
                        : ""}

                    {this.state.showOther == true ?
                        <TabPane tab="境外贸易商" key="6">{<Buyer wrappedComponentRef={(inst) => this.buyer = inst}
                            disabled={this.state.disabled}
                            buyerPage={this.props.buyerPage}
                            buyerList={this.props.buyerList}
                            cstid={this.state.cstid} dispatch={this.props.dispatch}
                            buyer={this.props.buyer} />}</TabPane>
                        : ""}


                    {this.state.showOther == true ?
                        <TabPane tab="产品管理" key="7">{<Product wrappedComponentRef={(inst) => this.product = inst}
                            disabled={this.state.disabled}
                            productPage={this.props.productPage}
                            productList={this.props.productList}
                            cstid={this.state.cstid} dispatch={this.props.dispatch}
                            product={this.props.product}
                            businessType={dataSource && dataSource.businessType ? dataSource.businessType : ""}/>}</TabPane>
                        : ""}

                    {this.state.showOther == true && dataSource.businessType != Constants.NUMBER_TWO ?
                        <TabPane tab="开票人管理" key="8">{<Drawer wrappedComponentRef={(inst) => this.drawer = inst}
                            disabled={this.state.disabled}
                            csttype={dataSource.csttype}
                            {...this.props.drawers}
                            cstid={this.state.cstid} dispatch={this.props.dispatch}
                        />}</TabPane>
                        : ""}


                    {this.state.showOther == true ?
                        <TabPane tab="订单管理" key="9">{<Order wrappedComponentRef={(inst) => this.order = inst}
                            disabled={this.state.disabled}
                            orderManage={this.props.orderManage}
                            cstid={this.state.cstid} dispatch={this.props.dispatch}
                        />}</TabPane>
                        : ""}



                    {this.state.showOther == true && !R.equals(dataSource.csttype, "2") ?
                        <TabPane tab="结算单" key="11">{<StatementList wrappedComponentRef={(inst) => this.statementList = inst}
                            disabled={this.state.disabled}
                            statement={this.props.statement}
                            capital={this.props.capital}
                            cstid={this.state.cstid} dispatch={this.props.dispatch}
                        />}</TabPane>
                        : ""}

                    {this.state.showOther && dataSource.tenantid === '0001' && !R.equals(dataSource.csttype, "2") ?
                        <TabPane tab="银企直联账号" key="12">{<H2HAccountList subAccountList={this.props.subAccountList} />}</TabPane>
                        : null}

                    {this.state.showOther == true && !R.equals(dataSource.csttype, "2") ?
                        <TabPane tab="客户对账单" key="13">{<ReconciliationList wrappedComponentRef={(inst) => this.reconciliationList = inst}
                            disabled={this.state.disabled}
                            dataSource={this.props.dataSource}
                            customerReport={this.props.customerReport}
                            cstid={this.state.cstid} dispatch={this.props.dispatch}
                        />}</TabPane>
                        : ""}


                    {this.state.showOther == true && !R.equals(dataSource.csttype, "2") ?
                        <TabPane tab="订单账目明细" key="14">{<OrderBilldetailList wrappedComponentRef={(inst) => this.orderBilldetailList = inst}
                            disabled={this.state.disabled}
                            dataSource={this.props.dataSource}
                            customerReport={this.props.customerReport}
                            cstid={this.state.cstid} dispatch={this.props.dispatch}
                        />}</TabPane>
                        : ""}
                </Tabs>
            </Modal>
        );
    }
}